package org.glut.AppStoreProject.controller;

import com.google.gson.Gson;
import org.glut.AppStoreProject.entity.App;
import org.glut.AppStoreProject.service.AppServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author Harry
 * @create time 2025/7/6 20:52
 */
@WebServlet("/list")
public class AppListServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("application/json;charset=UTF-8");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "GET, POST");
        PrintWriter writer = response.getWriter();

        writer.println();

            List<App> appList = new AppServiceImpl().getAppList();
            Gson gson = new Gson();
            String json = gson.toJson(appList);
            writer.write(json);
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        doGet(request, response);
    }
}